package org.oschina.api.dongtan.controller;

import org.oschina.api.BaseController;
import org.oschina.api.dongtan.model.Dongtan;
import org.oschina.api.dongtan.service.DongtanService;
import org.oschina.api.dongtan.vo.DongtanAccessVo;
import org.oschina.api.dongtan.vo.DongtanVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/dongtan")
public class DongtanController extends BaseController {

	@Autowired
	private DongtanService dongtanService;
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public DongtanAccessVo findById
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return dongtanService.findById(id);
	}
	
	/**
	 * 查询最新动弹
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/upToDate",method=RequestMethod.GET)
	public Pager findVoByUpToDate(Integer pn)
	{
		if(pn==null || pn<=0)
			return dongtanService.findVoByUpToDate();
		
		return dongtanService.findVoByUpToDate(pn);
	}
	
	/**
	 * 查询最热动弹
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/hot",method=RequestMethod.GET)
	public Pager findVoByHot(Integer pn)
	{
		if(pn==null || pn<=0)
			return dongtanService.findVoByHot();
		
		return dongtanService.findVoByHot(pn);
	}
	
	/**
	 * 发表动弹
	 * @param dongtan
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=
		{
			@ValidateField(name="content",validateValue=true,filter={TextOnlyFilter.class},min=5,max=200)
		}) Dongtan dongtan
	)
	{
		return dongtanService.create(dongtan);
	}
	
	/**
	 * 发表评论
	 * @param id
	 * @param content
	 * @return
	 */
	@Validate
	@RequestMapping(value="/comment/create",method=RequestMethod.POST)
	public Result createComment
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		String content,
		Integer quote
	)
	{
		if(quote==null || quote<0)
			quote=0;
		
		return dongtanService.createComment(id, content, quote);
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param content
	 * @return
	 */
	@Validate
	@RequestMapping(value="/edit/{id}",method=RequestMethod.POST)
	public Result edit
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		@Validate(validateField=@ValidateField(name="content",validateValue=true,filter={TextOnlyFilter.class},min=5,max=200)) String content
	)
	{
		return dongtanService.edit(id, content);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/delete/{id}",method=RequestMethod.POST)
	public Result delete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return dongtanService.delete(id);
	}
	
	/**
	 * 返回已删除
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public Pager<DongtanVo> findDelete(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return dongtanService.findDelete(pn);
	}
	
	/**
	 * 恢复已删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/recover/delete/{id}",method=RequestMethod.POST)
	public Result recoverDelete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return dongtanService.recoverDelete(id);
	}
}